﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Freshome.m.vws
{
    /// <summary>
    /// Majoraws.xaml 的交互逻辑
    /// </summary>
    public partial class Minoraws : UserControl
    {
        //private vms.Material _mt;
        public Minoraws()
        {
            InitializeComponent();
        }
        private const string _ep = "wsHttp_IMultint";
        private void Minoraw_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var dg = sender as DataGrid;
            if (dg != null)
            {
                if (dg.Name == "dgq") e.CanExecute = dgq.SelectedItem != null;
                else e.CanExecute = dgd.SelectedItem != null;
            }
            else e.CanExecute = false;
        }
        // 删除佐料
        private void Delete_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var dg = sender as DataGrid;
            var vm = this.DataContext as vms.Minoraws;
            if (dg != null && vm != null)
            {
                if (dg.Name == "dgd")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.SolidMaterial>, dgd.SelectedItem as mds.SolidMaterial, vm, "固态佐料");
                }
                else
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.LiquidMaterial>, dgq.SelectedItem as mds.LiquidMaterial, vm, "液态佐料");
                }
            }
        }
        private void Deletentity<T>(ObservableCollection<T> mis, T mi, vms.Minoraws vm, string titlead) where T : mds.Minoraw
        {
            if (mi != null && mis != null)
            {
                if (mi.Created == false)
                {
                    if (MessageBox.Show(string.Format("{0}({1})的数据将被删除，继续吗？", titlead, mi.Name), titlead,
                        MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        vm.Helproxy.Syncall(cln =>
                        {
                            if (cln.DeleMinoraw(mi.Raw.Mid)) mis.Remove(mi);
                        }, new Rmt.MultintClient(_ep));
                    }
                }
                else mis.Remove(mi);
            }
        }
    }
}
